﻿#include "servicesettingdialog.h"
#include "ui_servicesettingdialog.h"
#include <QSettings>
#include <QGlobalConfig>

ServiceSettingDialog::ServiceSettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ServiceSettingDialog)
{
    ui->setupUi(this);
    QSettings settings(QApplication::applicationDirPath()+"/config/httpservice.ini", QSettings::IniFormat);
    if(settings.value("listener/host").isNull()){
        ui->httpAddress->setText("0.0.0.0");
    }else{
        ui->httpAddress->setText(settings.value("listener/host").toString());
    }


    if(settings.value("listener/port").isNull()){
        ui->httpPort->setValue(3811);
    }else{
        ui->httpPort->setValue(settings.value("listener/port").toInt());
    }

    ui->httpOpenBox->setChecked(QGlobalConfig::getInstance().read("netWork","httpNetWorkOpen",true).toBool());
    ui->tcpOpenBox->setChecked(QGlobalConfig::getInstance().read("netWork","tcpSocketOpen",true).toBool());
    ui->udpOpenBox->setChecked(QGlobalConfig::getInstance().read("netWorke","udpSocketOpen",true).toBool());
    ui->webSocketOpenBox->setChecked(QGlobalConfig::getInstance().read("netWork","webSocketOpen",true).toBool());

    ui->tcpAddress->setText(QGlobalConfig::getInstance().read("tcpsocket","listenAdderss","0.0.0.0").toString());
    ui->tcpPort->setValue(QGlobalConfig::getInstance().read("tcpsocket","listenPort",3812).toInt());

    ui->udpBindPort->setValue(QGlobalConfig::getInstance().read("udpsocket","bindProt",3813).toInt());
    ui->udpProadcastAddress->setText(QGlobalConfig::getInstance().read("udpsocket","broadcastAddress","255.255.255.255").toString());
    ui->udpProadcastPort->setValue(QGlobalConfig::getInstance().read("udpsocket","broadcastPort",3813).toInt());

    ui->webSocketAddress->setText(QGlobalConfig::getInstance().read("websocket","listenAdderss","0.0.0.0").toString());
    ui->webSocketPort->setValue(QGlobalConfig::getInstance().read("websocket","listenPort",3814).toInt());

    ui->pushInterval->setValue(QGlobalConfig::getInstance().read("pushWork","pushInterval",2000).toInt());

    ui->httpGroupBox->setDisabled(!ui->httpOpenBox->isChecked());
    ui->tcpGroupBox->setDisabled(!ui->tcpOpenBox->isChecked());
    ui->udpGroupBox->setDisabled(!ui->udpOpenBox->isChecked());
    ui->webSocketGroupBox->setDisabled(!ui->webSocketOpenBox->isChecked());
}

ServiceSettingDialog::~ServiceSettingDialog()
{
    delete ui;
}

void ServiceSettingDialog::on_saveBtn_clicked()
{
    QSettings settings(QApplication::applicationDirPath()+"/config/httpservice.ini", QSettings::IniFormat);
    settings.setValue("listener/host",ui->httpAddress->text());
    settings.setValue("listener/port",ui->httpPort->value());

    QGlobalConfig::getInstance().write("netWork","httpNetWorkOpen", ui->httpOpenBox->isChecked());
    QGlobalConfig::getInstance().write("netWork","tcpSocketOpen",ui->tcpOpenBox->isChecked());
    QGlobalConfig::getInstance().write("netWorke","udpSocketOpen",ui->udpOpenBox->isChecked());
    QGlobalConfig::getInstance().write("netWork","webSocketOpen",ui->webSocketOpenBox->isChecked());

    QGlobalConfig::getInstance().write("tcpsocket","listenAdderss",ui->tcpAddress->text());
    QGlobalConfig::getInstance().write("tcpsocket","listenPort",ui->tcpPort->value());

    QGlobalConfig::getInstance().write("udpsocket","bindProt",ui->udpBindPort->value());
    QGlobalConfig::getInstance().write("udpsocket","broadcastAddress",ui->udpProadcastAddress->text());
    QGlobalConfig::getInstance().write("udpsocket","broadcastPort",ui->udpProadcastPort->value());

    QGlobalConfig::getInstance().write("websocket","listenAdderss",ui->webSocketAddress->text());
    QGlobalConfig::getInstance().write("websocket","listenPort",ui->webSocketPort->value());

    QGlobalConfig::getInstance().read("pushWork","pushInterval",ui->pushInterval->value());

    QGlobalConfig::getInstance().push();

    QDialog::accept();
}

void ServiceSettingDialog::on_exitBtn_clicked()
{
    QDialog::reject();
}

void ServiceSettingDialog::on_httpOpenBox_clicked(bool checked)
{
    ui->httpGroupBox->setDisabled(!checked);
}

void ServiceSettingDialog::on_tcpOpenBox_clicked(bool checked)
{
    ui->tcpGroupBox->setDisabled(!checked);
}

void ServiceSettingDialog::on_udpOpenBox_clicked(bool checked)
{
    ui->udpGroupBox->setDisabled(!checked);
}

void ServiceSettingDialog::on_webSocketOpenBox_clicked(bool checked)
{
    ui->webSocketGroupBox->setDisabled(!checked);
}
